// global for size of board
var gamesize = 12;
var done = new Array(); // array of 'flooded' elements
var allPcs = new Array();
// global vars for move count
var maxMoves = 22;
var moveCount = 0;
function generateBoard()
{
	for (var i = 0; i < gamesize; i++)
	{//ycoord
		for (var j = 0; j < gamesize; j++)
		{//xcoord
			makediv(i,j);	
		}
	}
}
function makediv(i,j)
{
	var board = document.getElementById("gameboard");
	var newdiv = document.createElement("div");
	var theID = 'p' + j + i; // id to use in arrays for comparison against doneEle's
	newdiv.id = theID;
	var random = Math.floor(Math.random()*6);
	switch(random)
	{
		// build initial color arrays to be used, contain all elements of a color
		// give class names
		case 0:
			newdiv.className = 'pieces green';
			break;
		case 1:
			newdiv.className = 'pieces purple';
			break;
		case 2:
			newdiv.className = 'pieces yellow';
			break;
		case 3:
			newdiv.className = 'pieces red';
			break;
		case 4:
			newdiv.className = 'pieces orange';
			break;
		case 5:
			newdiv.className = 'pieces blue';
			break;
		default:
			return;
	}
	board.appendChild(newdiv);
	allPcs.push(theID);
}
function startGame()
{
	$('#maxmoves').html(maxMoves); // place maxmoves onto the page
	generateBoard();
	done.push('#p00'); // first element in top left corner, its always 'done'
	updateDone();
	for (var i = 0; i < done.length; i++)
	{
		colorsMatch(done[i]);// call the initial join method to group ele's touching p00
	}
}
/***********************************************
*********************end build board************ 
***********************************************/
function reload()
{
//	history.go(0);
	killBoard();
	hider();
	startGame();
	moveCount = 0;
	$('#playermoves').html(moveCount);
}
function killBoard()
{
	var board = document.getElementById('gameboard');
	for (var i = 0; i < allPcs.length; i++)
	{
		var pc = allPcs[i].replace('#', '');
		var ele = document.getElementById(pc);
		board.removeChild(ele);
	}
	var cleararr = new Array();
	done = cleararr;
	allpcs = cleararr;
}
/********************************************************
 *******************end utility functions****************
/*******************************************************/

function colorsMatch (doneEle)
{
	var tempID = doneEle.replace('#p', '');
	var tempx = tempID[0];
	var tempy = tempID[1];
	if (tempID[2] != undefined)
	{
		alert(tempID[2]);
	}
	var dirs = new Array();

	dirs.push('#p' + tempx + String(parseInt(tempy) - 1));
	dirs.push('#p' + String(parseInt(tempx) + 1) + tempy);
	dirs.push('#p' + tempx + String(parseInt(tempy) + 1));
	dirs.push('#p' + String(parseInt(tempx) - 1) + tempy);
	for (var i = 0; i < dirs.length; i++)
	{
		if (dirs[i].search('-') == -1)
		{// check all dirs
			if ($(dirs[i]).css('background-color') == $(doneEle).css('background-color'))
			{
				if (!($(dirs[i]).hasClass('done')))
				{
					done.push(dirs[i]);
					updateDone();
				}
			}
		}
	}
	return;
}
function move(inputColor)
{
	for (var i = 0; i < done.length; i++)
	{// remove all classes from done elements, add input color
		var param = 'pieces done ' + inputColor;
		//alert(param);
		if ($(done[i]).hasClass('done'))
		{// element is in done and now has new color and 'pieces' 'done' and 'color'
			$(done[i]).removeClass();
			$(done[i]).addClass(param);
		}
	}
	for (var i = 0; i < done.length; i++)
	{
		colorsMatch(done[i]);
	}
	updateDone();
	moveCount++;
	checkMoves();
}
function checkMoves()
{
	$('#playermoves').html(moveCount);
	if (done.length == (gamesize * gamesize)) {
		displayr(1);
		return;
	}
	if (moveCount < maxMoves)
	{// moveCount is less than maxMoves. play on
		//increment move counter on page (1..maxMoves), not 0 based
		return;
	}
	else
	{// game is over
		//offer to play again, increment move counter to maxMoves 
		//do this with white content and fade like wedding site pictures
		displayr(0);
	}
}
function updateDone()
{
	for (var j = 0; j < done.length; j++)
	{// each element in done array
		if (!($(done[j]).hasClass('done')))
		{// if element doesnt have 'done' class, add it.
			$(done[j]).addClass('done');
		}
	}
}
function displayr(state)
{// shows lightbox and overlay background	
	$('#fade').fadeIn('slow');
	$('#light').fadeIn('slow');
	if (state == 0)
	{
		//white content should say game is over, offer to reload
	/*	var text = 'would you like to play again?';
		alert(text);
		$('#light p').html(text);*/
	//	document.getElementById('light').innerHTML = 'play again';
	}
	else
	{
		//white content should say you won, offer to reload
	//	document.getElementById('light').innerHTML = 'you win!';
	}
}
function hider()
{// hides lightbox and overlay background	
	$('#light').fadeOut('slow');
	$('#fade').fadeOut('slow');
}
